﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmMain : Form
    {
        #region Properties
        private clsFormManager FormManager { get; set; }
        public clsDataAccess DataAccess { get; set; }
        public delegate void OnTranslationChangedHandler();
        public OnTranslationChangedHandler Event_OnTranslationChanged;
        #endregion

        #region Constructors
        public frmMain()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        /// <summary>
        /// Starts all the startup processes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            //TODO: Insert into every method try and catch error handling and insert meaningful error discription.
            Boolean ShutdownProgram = false;

            //Initialise the FormManager and the DataAccess.
            this.FormManager = new clsFormManager(this);
            this.DataAccess = new clsDataAccess(Application.ExecutablePath);

            if (this.DataAccess.LoadConfiguration() == false)
            {
                //Occurs if there was no configuration or configuration file found.
                //Receive the configuration from the user.
                this.FormManager.ShowConfiguration();

                //If no configuration was inserted, the program is shut down.
                if (this.DataAccess.LoadConfiguration() == false)
                {
                    ShutdownProgram = true;
                }
            }

            if (ShutdownProgram == false)
            {
                //Start update and compare the program version against the database version.
                if (this.DataAccess.StartUpdate() == false)
                {
                    //The update process is not possible.
                    MessageBox.Show("Error 4. The program version must be updated.");
                    this.Close();
                }

                //Set all the menus needed for the administration during development.
                SetAdministrationMenu();

                //Show the login window.
                LogoutUser(false);
                if (this.DataAccess.UsersExists())
                {
                    FormManager.ShowLogin();
                }
                else
                {
                    //This occurs, when there are no users are found in the database.
                    //After setup the database has this state.
                    FormManager.ShowUsers();
                }
            }

            if (ShutdownProgram)
            {
                //Shutdown the program.
                this.Close();
            }
            else
            {
                Translate();
            }
        }

        /// <summary>
        /// Closes the complete program.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Opens the configurations window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void configurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowConfiguration();
        }

        /// <summary>
        /// Opens the stores window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void storesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowStores();
        }

        /// <summary>
        /// Opens the finances window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void financesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowFinances();
        }

        /// <summary>
        /// Opens the currency window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void currencyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowCurrency();
        }

        /// <summary>
        /// Opens the login window and logs out if logged in.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.DataAccess.Settings.User_Login == null)
            {
                FormManager.ShowLogin();
            }
            else
            {
                LogoutUser(true);
            }
        }

        /// <summary>
        /// Opens the users window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowUsers();
        }

        /// <summary>
        /// Opens the translation window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void translationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowTranslation();
        }

        /// <summary>
        /// Opens the languages window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void languagesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManager.ShowLanguages();
        }

        private void emptyTheDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.DataAccess.EmptyDatabase())
            {
                MessageBox.Show("The database has been emptied.");
                if (this.DataAccess.Settings.SaveLoginSettings(clsSettings.enumLoginMethod.AskForPassword))
                {
                    MessageBox.Show("The automatic login has been reset.");
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("While emptying the database an error occured.");
            }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Persue all the login configurations.
        /// </summary>
        /// <param name="pUser"></param>
        public void LoginUser(clsUser pUser)
        {
            this.DataAccess.Settings.User_Login = pUser;
            areaToolStripMenuItem.Visible = true;
            loginToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, loginToolStripMenuItem.Name + "_Case1", "Logout", 0);
            SetStatusLabel();
        }

        /// <summary>
        /// Persue all the logoff configurations.
        /// </summary>
        public void LogoutUser(Boolean pLoginInformationsToNull)
        {
            if (pLoginInformationsToNull)
            {
                this.DataAccess.Settings.User_Login = null;
            }
            SetStatusLabel();
            areaToolStripMenuItem.Visible = false;
            loginToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, loginToolStripMenuItem.Name + "_Case2", "Login", 0); ;
        }

        /// <summary>
        /// Translates every visible element.
        /// </summary>
        public void Translate()
        {
            this.Text = this.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            fileToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, fileToolStripMenuItem.Name, fileToolStripMenuItem.Text, 0);
            configurationToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, configurationToolStripMenuItem.Name, configurationToolStripMenuItem.Text, 0);
            usersToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, usersToolStripMenuItem.Name, usersToolStripMenuItem.Text, 0);
            translationToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, translationToolStripMenuItem.Name, translationToolStripMenuItem.Text, 0);
            closeToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, closeToolStripMenuItem.Name, closeToolStripMenuItem.Text, 0);
            areaToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, areaToolStripMenuItem.Name, areaToolStripMenuItem.Text, 0);
            financesToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, financesToolStripMenuItem.Name, financesToolStripMenuItem.Text, 0);
            storesToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, storesToolStripMenuItem.Name, storesToolStripMenuItem.Text, 0);
            languagesToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, languagesToolStripMenuItem.Name, languagesToolStripMenuItem.Text, 0);
            currencyToolStripMenuItem.Text = this.DataAccess.GetTranslation(this.Name, currencyToolStripMenuItem.Name, currencyToolStripMenuItem.Text, 0);

            SetStatusLabel();

            if (this.Event_OnTranslationChanged != null)
            {
                this.Event_OnTranslationChanged.Invoke();
            }
        }
        #endregion

        #region Private methods

        /// <summary>
        /// Sets the status label.
        /// </summary>
        /// <param name="AdditionalText">The additional text.</param>
        private void SetStatusLabel()
        {
            if (this.DataAccess.Settings.User_Login == null)
            {
                tlblStatus.Text = "";
            }
            else
            {
                if (this.DataAccess.Settings.User_Login.FirstName == null && this.DataAccess.Settings.User_Login.SecondName == null)
                {
                    tlblStatus.Text = "Not logged in.";
                }
                else
                {
                    tlblStatus.Text = this.DataAccess.GetTranslation(this.Name, tlblStatus.Name, "User:", 0) + " " + this.DataAccess.Settings.User_Login.FirstName + " " + this.DataAccess.Settings.User_Login.SecondName;
                }
            }
        }

        /// <summary>
        /// Sets all the menus used for administration while programming.
        /// </summary>
        private void SetAdministrationMenu()
        {

#if DEBUG
            administrationToolStripMenuItem.Visible = true;
#else
            administrationToolStripMenuItem.Visible = false;
#endif
        }
        #endregion
    }
}
